﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Collections;

namespace Editor.Old
{
    public abstract class Element : IComparable
    {
        public string Name { get; set; }

        public abstract IList ChildElements { get; set; }

        [XmlIgnore]
        public Element Parent { get; set; }

        [XmlIgnore]
        public virtual string Path
        {
            get
            {
                string path = Name;

                for (Element parent = Parent; parent != null; parent = parent.Parent)
                    path = System.IO.Path.Combine(parent.Name, path);

                return path;
            }
        }

        public Element()
        {
        }

        public Element(string name)
        {
            Name = name;
        }

        public abstract Element CreateChildElement(string childElementName);

        public virtual void AddChildElement(Element element)
        {
            element.Parent = this;
            ChildElements.Add(element);
        }

        public virtual Element AddChildElement(string elementName)
        {
            Element elem = CreateChildElement(elementName);

            AddChildElement(elem);

            return elem;
        }

        public virtual void RemoveChildElement(Element element)
        {
            ChildElements.Remove(element);
        }

        public virtual void RemoveChildElement(string elementName)
        {
            RemoveChildElement(CreateChildElement(elementName));
        }

        public void Remove()
        {
            if (Parent != null)
                Parent.RemoveChildElement(this);
        }

        public override string ToString()
        {
            return Name;
        }

        public int CompareTo(object obj)
        {
            if (GetType() != obj.GetType())
                throw new ArgumentException(GetType() + " türü ile " 
                    + obj.GetType() + " türü arasında mantıksız karşılaştırma!");

            return Path.CompareTo((obj as Element).Path);
        }

        public override bool Equals(object obj)
        {
            return GetType() == obj.GetType() && Path.Equals((obj as Element).Path);
        }

        public override int GetHashCode()
        {
            return Path.GetHashCode();
        }
    }
}
